package com.xcw;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author bhx
 * @Date 2022/11/8 2:41
 * @Description TODO
 */
public class Tst {
    public void AllByType(Object model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("根据类型来遍历对象的属性");
        java.lang.reflect.Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性，返回Field数组
        for(int j=0 ; j<field.length ; j++){     //遍历所有属性
            String name = field[j].getName();    //获取属性的名字

//            System.out.println("attribute name:"+name);
            name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
            String type = field[j].getGenericType().toString();    //获取属性的类型
            if(type.equals("class java.lang.String")){   //如果type是类类型，则前面包含"class "，后面跟类名
                java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
                String value = (String) m.invoke(model);    //调用getter方法获取属性值
                if(value != null){
                    System.out.println("字符串类型 :"+value);
                } else {
                    System.out.println("字符串类型 :"+value);
                }
            }
            if(type.equals("int")||type.equals("class java.lang.Integer")){
                java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
                Integer value = (Integer) m.invoke(model);
                if(value != null){
                    System.out.println("int类型 :"+value);
                }else {
                    System.out.println("int类型 :"+value);

                }
            }
            if(type.equals("class java.lang.Boolean")||type.equals("boolean")){
                if (type.equals("class java.lang.Boolean")){
                    java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
                    Boolean value = (Boolean) m.invoke(model);
                    if(value != null){
                        System.out.println("布尔类型:"+value);
                    } else {
                        System.out.println("布尔类型:"+value);
                    }
                }else{
                    java.lang.reflect.Method m = model.getClass().getMethod("is"+name);
                    boolean value = (boolean) m.invoke(model);
                    if(value){
                        System.out.println("布尔类型:"+value);
                    } else {
                        System.out.println("布尔类型:"+value);
                    }
                }

            }
        }
    }

    //将所有的属性转换对象
    public void AllByObject(Object model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        System.out.println("将所有的属性转换对象");
        java.lang.reflect.Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性，返回Field数组
        for(int j=0 ; j<field.length ; j++){     //遍历所有属性
            String name = field[j].getName();    //获取属性的名字
            System.out.println("attribute name:"+name);
            name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
            java.lang.reflect.Method m = model.getClass().getMethod("get"+name);
            Object value = (Object) m.invoke(model);    //调用getter方法获取属性值
            if(value != null){
                System.out.println("attribute value:"+value.toString());
            }
        }
    }

    //get方法
    public void allByGet(Object model) throws IllegalArgumentException, IllegalAccessException{
        System.out.println("get方法");
        java.lang.reflect.Field[] flds = model.getClass().getFields();
        if ( flds != null )
        {
            for ( int i = 0; i < flds.length; i++ )
            {
                System.out.println(flds[i].getName() + " - " + flds[i].get(model));
            }
        }
    }

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        User model = new User();
        Tst output = new Tst();
        output.AllByType(model);

    }

}
